﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.world {
    import flash.events.*;
    import reprise.commands.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import __AS3__.vec.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.*;
    import reprise.tweens.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import com.robertpenner.easing.*;

    public class RoomView extends AbstractAreaView implements ISnapArea {

        private var _facade:Sprite;
        private var _facadeContainer:Sprite;
        private var _highlightTween:SimpleTween;
        private var _darkenTween:SimpleTween;
        private var _highlightRect:Sprite;
        private var _darkendRoomRect:Sprite;
        private var _snapareaFacade:Sprite;
        private var _snapareaCeiling:Sprite;
        private var _snapareaWall:Sprite;
        private var _snapareaFloor:Sprite;
        private var _isBuildMode:Boolean;
        private var _elements:Dictionary;
        private var _highlightMachineTimer:Timer;
        private var _machine:IMachineView;
        private var _rewardBackgroundAnimation:MovieClip;
        private var _envelopes:DictStorage;
        private var _hireDoctorButton:MovieClip;
        private var _rewardStartPos:Point;
        private var _rewardLandingXStart:int;
        private var _rewardLandingXEnd:int;
        private var _outsideMc:MovieClip;

        private static const HIRE_DOCTOR_BUTTON:String = "HireDoctorButton";
        private static const HIRE_DOCTOR_BUTTON_FRIEND_PADDING:int = 150;

        public function RoomView(_arg1:String, _arg2:Number, _arg3:Number, _arg4:String){
            this._elements = new Dictionary(true);
            this._envelopes = new DictStorage();
            super(_arg1, _arg2, _arg3, _arg4);
            if (_asset){
                this.init();
            };
        }
        protected function init():void{
            this._outsideMc = AssetsLibrary.getMovieClip("FacadeCabin");
            this._facade = (_asset.addChild(this._outsideMc) as MovieClip);
            this._facade.name = ShopItemVO.DESTINATION_FACADE;
            this._facade.visible = false;
            this._facadeContainer = new Sprite();
            this._facadeContainer.name = ShopItemVO.DESTINATION_FACADE;
            this._facade.addChild(this._facadeContainer);
            this._snapareaCeiling = ((Sprite(_asset.snaparea_ceiling)) || (this.createSnaparea("snap_ceiling", 0, 0, 760, 1)));
            this._snapareaWall = ((Sprite(_asset.snaparea_wall)) || (this.createSnaparea("snap_wall", 0, 0, 760, 330)));
            this._snapareaFloor = ((Sprite(_asset.snaparea_floor)) || (this.createSnaparea("snap_floor", 0, 330, 760, 130)));
            this._snapareaFacade = ((Sprite(_asset.snaparea_facade)) || (this.createSnaparea("snap_facade", 0, 0, 760, 460)));
            this._snapareaCeiling.visible = (this._snapareaWall.visible = (this._snapareaFloor.visible = (this._snapareaFacade.visible = false)));
            var _local1:Sprite = Sprite(_asset.getChildByName("frame"));
            if (_local1){
                _local1.mouseEnabled = (_local1.mouseChildren = false);
            };
            this.isBuildMode = false;
            this._rewardBackgroundAnimation = AssetsLibrary.getMovieClip("RewardAnimationBackground");
            this._rewardBackgroundAnimation.stop();
            this._rewardBackgroundAnimation.visible = false;
            addChild(this._rewardBackgroundAnimation);
            cacheAsBitmap = true;
        }
        public function initMachine(_arg1:String, _arg2:String, _arg3:Class):void{
            var _local4:MovieClip;
            var _local5:DisplayObjectContainer;
            if (((_arg2) && (_arg3))){
                _local4 = (Displays.getChild(_asset, _arg2) as MovieClip);
                _local5 = _local4.parent;
                this._machine = new _arg3(_arg1, _local4);
                this._machine.x = this._machine.content.x;
                this._machine.y = this._machine.content.y;
                this._machine.content.x = 0;
                this._machine.content.y = 0;
                _local5.addChild(DisplayObject(this._machine));
            };
        }
        public function facadeVisible(_arg1:Boolean):void{
            this._facade.visible = _arg1;
        }
        public function setDarkenedRoomVisible(_arg1:Boolean):void{
            if (this._darkendRoomRect.visible){
                this._darkendRoomRect.alpha = (_arg1) ? 1 : 0;
            };
        }
        public function addElement(_arg1:ElementVO):void{
            var vo = _arg1;
            var element:ElementDisplay = new ElementDisplay();
            element.name = vo.id;
            try {
                element.asset = AssetsLibrary.getMovieClip(vo.type);
            } catch(e:Error) {
                log(vo.type);
            };
            element.x = vo.x;
            element.y = vo.y;
            element.depth = vo.z;
            element.mouseChildren = false;
            element.isBackground = vo.isBackground;
            this.addToDestination(vo.destination, element);
            switch (vo.destination){
                case ShopItemVO.DESTINATION_FLOOR:
                    this.sortArea(this.floor, "y");
                    break;
                case ShopItemVO.DESTINATION_WALL:
                    this.sortArea(this.wall, "depth");
                    break;
                case ShopItemVO.DESTINATION_CEILING:
                    this.sortArea(this.ceiling, "depth");
                    break;
                case ShopItemVO.DESTINATION_FACADE:
                    this.sortArea(this._facadeContainer, "depth");
                    break;
            };
            this._elements[vo.id] = element;
            this.activateElement(element, this._isBuildMode);
        }
        public function removeElement(_arg1:ElementVO):void{
            Displays.removeChildByName(this.getContainer(_arg1.destination), _arg1.id);
            delete this._elements[_arg1.id];
        }
        private function onAnimation(_arg1:Event):void{
            var _local2:MovieClip = (_arg1.currentTarget as MovieClip);
            if (_local2.currentFrameLabel == "end"){
                _local2.removeEventListener(Event.ENTER_FRAME, this.onAnimation);
                _local2.x = _local2.endX;
                this._rewardBackgroundAnimation.visible = false;
            };
            _local2.x = (_local2.x + ((_local2.endX - _local2.startX) / _local2.framesLoaded));
        }
        private function addToDestination(_arg1:String, _arg2:ElementDisplay):void{
            var _local3:DisplayObjectContainer = this.getContainer(_arg1);
            if (_local3){
                _local3.addChild(_arg2);
            };
        }
        private function getContainer(_arg1:String):DisplayObjectContainer{
            if (_arg1 == ShopItemVO.DESTINATION_FACADE){
                return (this._facadeContainer);
            };
            return (DisplayObjectContainer(_asset.getChildByName(_arg1)));
        }
        private function sortArea(_arg1:DisplayObjectContainer, _arg2:String):void{
            var _local3:Array = Displays.getChildren(_arg1);
            _local3.sortOn(_arg2, Array.NUMERIC);
            var _local4:ElementDisplay = Arrays.extractByAttribute(_local3, "isBackground", true);
            if (_local4){
                _local3.unshift(_local4);
            };
            Displays.setIndices(_local3);
        }
        public function getDestinationRect(_arg1:String):Rectangle{
            var _local2:DisplayObject = _asset.getChildByName(("snap_" + _arg1));
            return (_local2.getRect(stage));
        }
        private function onElementOver(_arg1:MouseEvent):void{
            var _local2:ElementDisplay = ElementDisplay(_arg1.currentTarget);
            _local2.filters = [Filters.getFilter(Filters.YELLOW_OUTLINE)];
        }
        private function onElementOut(_arg1:MouseEvent):void{
            var _local2:ElementDisplay = ElementDisplay(_arg1.currentTarget);
            _local2.filters = Filters.NO_FILTERS;
        }
        private function onElementClicked(_arg1:MouseEvent):void{
            _arg1.stopPropagation();
            var _local2:ElementDisplay = ElementDisplay(_arg1.currentTarget);
            _local2.filters = Filters.NO_FILTERS;
            _local2.removeEventListener(MouseEvent.CLICK, this.onElementClicked);
            _local2.removeEventListener(MouseEvent.MOUSE_OVER, this.onElementOver);
            _local2.removeEventListener(MouseEvent.MOUSE_OUT, this.onElementOut);
            var _local3:ElementEvent = new ElementEvent(ElementEvent.MOVE_ELEMENT, _local2);
            _local3.destination = _local2.parent.name;
            delete this._elements[_local2.name];
            Displays.removeChild(_local2);
            dispatchEvent(_local3);
        }
        private function createSnaparea(_arg1:String, _arg2:int, _arg3:int, _arg4:int, _arg5:int, _arg6:int=-1):Sprite{
            var _local7:Sprite = new Sprite();
            _local7.name = _arg1;
            _local7.x = _arg2;
            _local7.y = _arg3;
            Drawings.drawRect(_local7.graphics, 0, 0, _arg4, _arg5);
            if (_arg6 >= 0){
                _asset.addChildAt(_local7, _arg6);
            } else {
                _asset.addChild(_local7);
            };
            return (_local7);
        }
        public function initSnaparea():void{
            mouseChildren = false;
            addEventListener(MouseEvent.MOUSE_OVER, this.onSnapIn);
            addEventListener(MouseEvent.MOUSE_OUT, this.onSnapOut);
        }
        public function cleanup():void{
            mouseChildren = true;
            removeEventListener(MouseEvent.MOUSE_OVER, this.onSnapIn);
            removeEventListener(MouseEvent.MOUSE_OUT, this.onSnapOut);
        }
        private function onSnapIn(_arg1:MouseEvent):void{
            var _local2:SnapEvent = new SnapEvent(EventConstants.SNAP_IN, this);
            dispatchEvent(_local2);
        }
        private function onSnapOut(_arg1:MouseEvent):void{
            var _local2:Event = new Event(EventConstants.SNAP_OUT);
            dispatchEvent(_local2);
        }
        private function get machine():MovieClip{
            return ((this.floor) ? MovieClip(this.floor.machine) : null);
        }
        public function get asset():MovieClip{
            return (_asset);
        }
        public function highlightMachine():void{
            if (this.machine){
                this._highlightMachineTimer = new Timer(300, 6);
                this._highlightMachineTimer.addEventListener(TimerEvent.TIMER, this.onHighlightMachineTick);
                this._highlightMachineTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onHighlightMachineComplete);
                this._highlightMachineTimer.start();
            };
        }
        private function onHighlightMachineTick(_arg1:TimerEvent):void{
            this.machine.mouse_over_frame.visible = !(this.machine.mouse_over_frame.visible);
        }
        private function onHighlightMachineComplete(_arg1:TimerEvent):void{
            this.machine.mouse_over_frame.visible = false;
            this._highlightMachineTimer.stop();
            this._highlightMachineTimer.removeEventListener(TimerEvent.TIMER, this.onHighlightMachineTick);
            this._highlightMachineTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.onHighlightMachineComplete);
        }
        public function enableDarkenRoom():void{
            this._darkendRoomRect.visible = true;
            this._darkenTween = new SimpleTween(1000);
            this._darkenTween.addTweenProperty(this._darkendRoomRect, "alpha", 0, 1, Linear.easeOut);
            this._darkenTween.addEventListener(Event.COMPLETE, this.onDarkenTweenInComplete);
            this._darkenTween.execute();
        }
        public function disableDarkenRoom():void{
            this._darkenTween = new SimpleTween(1000);
            this._darkenTween.addTweenProperty(this._darkendRoomRect, "alpha", 1, 0, Linear.easeOut);
            this._darkenTween.addEventListener(Event.COMPLETE, this.onDarkenTweenOutComplete);
            this._darkenTween.execute();
        }
        private function onDarkenTweenOutComplete(_arg1:Event):void{
            if (this._darkenTween){
                this._darkenTween.removeEventListener(Event.COMPLETE, this.onDarkenTweenOutComplete);
                this._darkendRoomRect.visible = false;
            };
        }
        private function onDarkenTweenInComplete(_arg1:Event):void{
            if (this._darkenTween){
                this._darkenTween.removeEventListener(Event.COMPLETE, this.onDarkenTweenInComplete);
                if (this._facade.visible){
                    this._darkendRoomRect.alpha = 0;
                };
            };
        }
        public function highlightRoom():void{
            if (this._highlightTween){
                if (this._highlightTween.getTime() > 0){
                    return;
                };
            };
            this._highlightRect.visible = true;
            this._highlightTween = new SimpleTween(1500);
            this._highlightTween.addTweenProperty(this._highlightRect, "alpha", 1, 0, Linear.easeOut);
            this._highlightTween.addEventListener(Event.COMPLETE, this.onHighlightTweenComplete);
            this._highlightTween.execute();
        }
        private function onHighlightTweenComplete(_arg1:Event):void{
            if (this._highlightTween){
                this._highlightTween.removeEventListener(Event.COMPLETE, this.onHighlightTweenComplete);
                this._highlightRect.visible = false;
                this._highlightTween = null;
            };
        }
        override protected function createChildren():void{
            super.createChildren();
            this.createHightlightRect();
            this.createDarkendRoomRect();
            this.createHireDoctorButton();
        }
        private function createDarkendRoomRect():void{
            var _local1:Number = (width / 3);
            var _local2:Number = ((width / 3) * 2);
            this._darkendRoomRect = new Sprite();
            this._darkendRoomRect.graphics.beginFill(0, 0.6);
            this._darkendRoomRect.graphics.drawPath(Vector.<int>([1, 2, 2, 2]), Vector.<Number>([0, 0, _local1, 0, 0, height, 0, 0]));
            this._darkendRoomRect.graphics.drawPath(Vector.<int>([1, 2, 2, 2]), Vector.<Number>([_local2, 0, width, 0, width, height, _local2, 0]));
            this._darkendRoomRect.graphics.endFill();
            this._darkendRoomRect.graphics.beginFill(0, 0.4);
            this._darkendRoomRect.graphics.drawPath(Vector.<int>([1, 2, 2, 2, 2]), Vector.<Number>([_local1, 0, _local2, 0, width, height, 0, height, _local1, 0]));
            this._darkendRoomRect.graphics.endFill();
            this._darkendRoomRect.visible = false;
            this._darkendRoomRect.mouseEnabled = false;
            _asset.addChild(this._darkendRoomRect);
        }
        private function createHightlightRect():void{
            this._highlightRect = new Sprite();
            this._highlightRect.graphics.beginFill(0xFFFFFF, 0.7);
            this._highlightRect.graphics.lineStyle(1, 0xFFFFFF, 0);
            this._highlightRect.graphics.drawRect(0, 0, width, height);
            this._highlightRect.graphics.endFill();
            this._highlightRect.visible = false;
            this._highlightRect.mouseEnabled = false;
            addChild(this._highlightRect);
        }
        public function set isBuildMode(_arg1:Boolean):void{
            var _local2:Function;
            this._isBuildMode = _arg1;
            this.activateElements(this._isBuildMode);
            if (_asset){
                _local2 = (this._isBuildMode) ? _asset.removeEventListener : _asset.addEventListener;
                _local2(MouseEvent.CLICK, this.background_click);
            };
        }
        private function activateElements(_arg1:Boolean):void{
            var _local2:ElementDisplay;
            for each (_local2 in this._elements) {
                this.activateElement(_local2, _arg1);
            };
        }
        private function activateElement(_arg1:ElementDisplay, _arg2:Boolean):void{
            _arg1.draggable = _arg2;
            _arg1.removeEventListener(MouseEvent.CLICK, this.onElementClicked);
            _arg1.removeEventListener(MouseEvent.MOUSE_OVER, this.onElementOver);
            _arg1.removeEventListener(MouseEvent.MOUSE_OUT, this.onElementOut);
            if (_arg2){
                _arg1.addEventListener(MouseEvent.CLICK, this.onElementClicked);
                _arg1.addEventListener(MouseEvent.MOUSE_OVER, this.onElementOver);
                _arg1.addEventListener(MouseEvent.MOUSE_OUT, this.onElementOut);
            };
        }
        private function background_click(_arg1:MouseEvent):void{
            _arg1.stopPropagation();
            dispatchEvent(new Event(EventConstants.ROOM_CLICKED));
        }
        private function get floor():MovieClip{
            return ((_asset) ? (_asset.floor as MovieClip) : null);
        }
        private function get wall():MovieClip{
            return ((_asset) ? (_asset.wall as MovieClip) : null);
        }
        private function get ceiling():MovieClip{
            return ((_asset) ? (_asset.ceiling as MovieClip) : null);
        }
        public function addEnvelope(_arg1:String, _arg2:int, _arg3:int):MovieClip{
            if (this._envelopes.has(_arg1)){
                return (this._envelopes.get(_arg1));
            };
            var _local4:MovieClip = MovieClip(_asset.addChild(AssetsLibrary.getMovieClip(GameConstants.ENVELOPE)));
            _local4.visible = false;
            _local4.buttonMode = true;
            _local4.name = _arg1;
            _local4.x = _arg2;
            _local4.y = _arg3;
            this._envelopes.set(_arg1, _local4);
            return (_local4);
        }
        public function removeEnvelope(_arg1:String):void{
            var _local2:MovieClip = this._envelopes.get(_arg1);
            _local2.stop();
            _asset.removeChild(_local2);
            _local2 = null;
        }
        public function setEnvelopesVisible(_arg1:Boolean, _arg2:int=0):void{
            var isVisible = _arg1;
            var delay = _arg2;
            var delayTimer:TimerCommand = new TimerCommand(delay);
            delayTimer.addEventListener(Event.COMPLETE, function (_arg1:Event):void{
                var _local3:MovieClip;
                var _local2:Array = _envelopes.values;
                for each (_local3 in _local2) {
                    _local3.visible = isVisible;
                };
            });
            delayTimer.execute();
        }
        public function showHireDoctorButton(_arg1:String):void{
            if (!_asset.contains(this.hireDoctorButton)){
                _asset.addChild(this.hireDoctorButton);
            };
            this.setHireDoctorText(_arg1);
        }
        public function hideHireDoctorButton():void{
            _asset.removeChild(this.hireDoctorButton);
        }
        private function createHireDoctorButton():void{
            this._hireDoctorButton = AssetsLibrary.getMovieClip(HIRE_DOCTOR_BUTTON);
            this._hireDoctorButton.friendBox.visible = false;
            this._hireDoctorButton.gotoAndStop("default");
            this._hireDoctorButton.mouseEnabled = (this._hireDoctorButton.buttonMode = (this._hireDoctorButton.useHandCursor = true));
            this._hireDoctorButton.mouseChildren = false;
        }
        private function setHireDoctorText(_arg1:String):void{
            var text = _arg1;
            var format:TextFormat = new TextFormat();
            format.font = "MyriadProFont";
            format.align = TextFormatAlign.CENTER;
            format.bold = true;
            format.color = "0xffffff";
            format.size = 52;
            var _local3 = this._hireDoctorButton;
            with (_local3) {
                label.embedFonts = true;
                label.htmlText = text;
                label.autoSize = TextFieldAutoSize.CENTER;
                label.multiline = true;
                label.setTextFormat(format);
            };
            this.centerHireDoctorButton();
        }
        private function centerHireDoctorButton():void{
            this._hireDoctorButton.x = ((this.width / 2) - (this._hireDoctorButton.width / 2));
            this._hireDoctorButton.y = ((this.height / 2) - (this._hireDoctorButton.height / 2));
        }
        public function addHireDoctorThumb(_arg1:String, _arg2:String):void{
            var _local3:MovieClip = AssetsLibrary.getMovieClip((_arg1 + "DoctorThumb"));
            this._hireDoctorButton.friendBox.friendPicture.addChild(_local3);
            this._hireDoctorButton.friendBox.visible = true;
            this.setDoctorName(_arg2);
            this.centerHireDoctorButton();
            this._hireDoctorButton.y = (this._hireDoctorButton.y + HIRE_DOCTOR_BUTTON_FRIEND_PADDING);
        }
        private function setDoctorName(_arg1:String):void{
            var doctorName = _arg1;
            var format:TextFormat = new TextFormat();
            format.font = "MyriadProFont";
            format.align = TextFormatAlign.CENTER;
            format.color = "0xBE9476";
            format.size = 12;
            var _local3 = this._hireDoctorButton.friendBox;
            with (_local3) {
                friendName.embedFonts = true;
                friendName.htmlText = doctorName;
                friendName.autoSize = TextFieldAutoSize.CENTER;
                friendName.multiline = true;
                friendName.setTextFormat(format);
            };
        }
        public function addHiredDoctorAnimation(_arg1:MovieClip):void{
            _asset.addChild(_arg1);
        }
        public function get hireDoctorButton():MovieClip{
            return (this._hireDoctorButton);
        }
        public function setRewardPositions(_arg1:Point, _arg2:int, _arg3:int):void{
            this._rewardStartPos = _arg1;
            this._rewardLandingXStart = _arg2;
            this._rewardLandingXEnd = _arg3;
        }

    }
}//package net.wooga.pets.views.world 
